package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysDeptMapper;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysDeptExample;
import com.sofwin.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @author: andyliu
 * @date: 2022/8/4 16:17
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service("deptService")
public class DeptServiceImpl implements DeptService {
    @Autowired
    private SysDeptMapper mapper;

    @Override
    public boolean saveDept(SysDept dept) {
        if(dept.getId()!=null){
            return mapper.updateByPrimaryKeySelective(dept)==1?true:false;
        }
        return mapper.insertSelective(dept)==1?true:false;
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public SysDept queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysDept> queryByPage(SysDept dept, Integer pageNumber, Integer pageSize) {
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(dept.getDeptName())){
            criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<SysDept> depts = mapper.selectByExample(example);
        return new PageInfo<>(depts);
    }

    @Override
    public List<SysDept> queryDepts(SysDept dept) {
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(dept.getDeptName())){
            criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
        }
        List<SysDept> depts = mapper.selectByExample(example);
        return depts;
    }
}
